package com.hncc.kumao.biz.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.hncc.kumao.biz.IUserBiz;
import com.hncc.kumao.dao.IUserDao;
import com.hncc.kumao.entity.User;
import com.hncc.kumao.exception.KumaoException;
import com.hncc.kumao.util.ConstValue;
import com.hncc.kumao.util.MD5Utils;
import com.hncc.kumao.util.MyFileUtils;

import jcifs.dcerpc.msrpc.netdfs;

/**
 * 用户业务层类
 * @author chen
 *
 */
public class UserBiz implements IUserBiz {

	private IUserDao userDao;
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	
	/**
	 * 用户注册
	 */
    public String regist(User user) {
        //判断电话是否以注册
        User u = new User();
        u.setPhone(user.getPhone());
        long count = userDao.getCount(u, null, null);
        if(count > 0) {
            throw new KumaoException("该手机号已被注册");
        }
        //初始化一下数据
        user.setRegisterDate(new Date());
        user.setState(0);
        user.setHeadPortraitUrl("/" + ConstValue.IMAGE_DIR_NAME + "/def.jpg");
        //user.setUsername(" ");
        //user.setEmail(" ");
        //加密密码
        user.setPassword(MD5Utils.md5(user.getPassword()));
        //调用持久层
        String id = userDao.add(user);
        return id;
    }

    /**
     * 根据id获取
     */
    public User getById(String id) {
        return userDao.get(id);
    }

    /**
     * 根据手机号获取
     */
    public User getByPhone(String phone) {
        User user = new User();
        user.setPhone(phone);
        return userDao.getList(user, null, null).get(0);
    }

    /**
     * 根据手机号和密码获取
     */
    public User getByPhoneAndPassword(String phone, String password) {
        User user = new User();
        user.setPhone(phone);
        user.setPassword(MD5Utils.md5(password));
        User loginUser = null;
        try {
            loginUser = userDao.getList(user, null, null).get(0);
        } catch (Exception e) {
            e.printStackTrace();
            throw new KumaoException("手机号或密码错误");
        }
        return loginUser;
    }

    /**
     * 设置用户信息,主要是设置用户名和邮箱
     */
    public User setUserInfo(String id, User user) {
        //查询出要更新的用户
        User updteUser = userDao.get(id);
        //判断用户名和邮箱是否重复
        User u = new User();
        u.setUsername(user.getUsername());
        long count = userDao.getCount(u, null, null);
        if(count > 0 && !updteUser.getUsername().equals(user.getUsername())) {
            throw new KumaoException("该用户名已存在");
        }
        u = new User();
        u.setEmail(user.getEmail());
        count = userDao.getCount(u, null, null);
        if(count > 0 && !updteUser.getEmail().equals(user.getEmail())) {
            throw new KumaoException("该邮箱已被注册");
        }
        //没问题，调用dao层进行更新
        updteUser.setUsername(user.getUsername());
        updteUser.setEmail(user.getEmail());
        if(user.getHeadPortraitUrl() != null 
                && user.getHeadPortraitUrl().trim().length() > 0) {
            updteUser.setHeadPortraitUrl(user.getHeadPortraitUrl());            
        }
        userDao.update(updteUser);
        return userDao.get(id);
    }
    
    /**
     * 获取用户列表
     */
    public List<User> getList(User user1, User user2, Object param) {
        List<User> list = userDao.getList(user1, user2, param);
        initUserSpaceSize(list);
        return list;
    }

    /**
     * 分页查询用户列表
     */
    public List<User> getListByPage(User user1, User user2, Object param, int firstResult, int maxResults) {
        List<User> list = userDao.getListByPage(user1, user2, param, firstResult, maxResults);
        initUserSpaceSize(list);
        return list;
    }
    
    /**
     * 统计记录个数
     */
    public long getCount(User user1, User user2, Object param) {
        return userDao.getCount(user1, user2, param);
    }
    
    private void initUserSpaceSize(List<User> list) {
        for (User user : list) {
            Long totlaSize = 1024l * 1024l * 1024l * 5l;
            user.setTotalSize(totlaSize);
            String downloadPath = ConstValue.BASE_RES_PATH + "/" + ConstValue.DOWNLOAD_DIR_NAME + "/" + user.getId();
            String uploadPath = ConstValue.BASE_RES_PATH + "/" + ConstValue.UPLOAD_DIR_NAME + "/" + user.getId();
            File file = new File(downloadPath);
            Long useSize = MyFileUtils.getDirSize(file);
            file = new File(uploadPath);
            useSize += MyFileUtils.getDirSize(file);
            user.setUseSize(useSize);
        }
    }

}
